package com.jky.trial.controller;

import com.jky.trial.common.BaseResult;
import com.jky.trial.common.Constants;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.jwt.JWTUtil;
import com.jky.trial.entity.Log;
import com.jky.trial.entity.User;
import com.jky.trial.service.LogService;
import com.jky.trial.vo.LogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/v1/log")
public class LogController {

    private LogService recordService;

    @Autowired
    LogController(LogService recordService) {
        this.recordService = recordService;
    }

    @PostMapping
    public BaseResult create(@RequestBody Log record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        record.setUserId(user.getId());//记录人
        recordService.create(record);
        return new BaseResult(true, record.getId());
    }

    @PutMapping
    public BaseResult update(@RequestBody Log record) {
        return new BaseResult(true, recordService.update(record));
    }

    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    @GetMapping("/{id}")
    public BaseResult get(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @GetMapping
    public ListResult page(LogVo record) {
        return recordService.page(record);
    }

    @GetMapping("/pageWithUser")
    public ListResult pageWithUser(LogVo record) {
        return recordService.pageWithUser(record);
    }

    @GetMapping("/list")
    public ListResult list(LogVo record) {
        return new ListResult(true, recordService.list(record));
    }

    /**
     * 设备出借记录
     *
     * @param record
     * @return
     */
    @GetMapping("/deviceRecord")
    public ListResult deviceRecord(LogVo record) {
        return recordService.deviceRecord(record);
    }
}
